﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace BenhVien
{
    public partial class PhieuHen : Form
    {
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter();
        Connect conn = new Connect();
        public PhieuHen()
        {
            InitializeComponent();
        }
        public void getdata() 
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from PhieuHen";
            da.SelectCommand = command;
            da.Fill(dt);
            dataGridView.DataSource = dt;

        }
       
        private void bt_them_Click(object sender, EventArgs e)
        {
            if (txtMaPH.Text == "" | txtTenNV.Text == "" | txtMaNV.Text == "" | txtPhongKT.Text == ""| txtNam.Text==""|txtNgay.Text==""|cbGiay.Text==""|cbGio.Text==""|cbPhut.Text==""|cbThang.Text=="")
                MessageBox.Show("Bạn Chưa Nhập Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK);
            else
            {
                if (check("Select MaPhieuHen from PhieuHen where MaPhieuHen =@DoiSo", txtMaPH.Text)==true)
                    MessageBox.Show("Ma bi trung", "Loi", MessageBoxButtons.OK);
                else
                    {
                        if (check("select MaNV from NhanVien where MaNV = @DoiSo ", txtMaNV.Text) == false)
                            MessageBox.Show("Mã nhân viên không tồn tại", "lỗi", MessageBoxButtons.OK);
                        else
                        {
                            DataRow row = dt.NewRow();
                            row["MaPhieuHen"] = txtMaPH.Text;
                            row["TenNV"] = txtTenNV.Text;
                            row["MaNV"] = txtMaNV.Text;
                            //row["NgayKiemTra"] = dateTimePicker1.Value;
                            row["NgayKiemTra"] = getdate();
                            row["PhongKiemTra"] = txtPhongKT.Text;
                            dt.Rows.Add(row);
                            SqlCommand commandInsert = new SqlCommand();
                            commandInsert.Connection = conn.con;
                            commandInsert.CommandType = CommandType.Text;
                            commandInsert.CommandText = @"Insert PhieuHen (MaPhieuHen, TenNV, MaNV, NgayKiemTra, PhongKiemTra) Values 
                                                ( @MaPhieuHen, @TenNV,  @MaNV, @NgayKiemTra, @PhongKiemTra)";
                            commandInsert.Parameters.Add("@MaPhieuHen", SqlDbType.NVarChar, 50, "MaPhieuHen");
                            commandInsert.Parameters.Add("@TenNV", SqlDbType.NVarChar, 50, "TenNV");
                            commandInsert.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                            commandInsert.Parameters.Add("@NgayKiemTra", SqlDbType.Date, 50, "NgayKiemTra");
                            commandInsert.Parameters.Add("@PhongKiemTra", SqlDbType.NVarChar, 50, "PhongKiemTra");
                            da.InsertCommand = commandInsert;
                            da.Update(dt);
                            MessageBox.Show("Ban da them Thanh Cong", "chuc Mung", MessageBoxButtons.OK);
                        }
                }
            }
        }

        private bool check(String s, String s1)
        {
            bool check1 = false;
            DataTable data = new DataTable();
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = s;
            command.Parameters.Add("@DoiSo", SqlDbType.NVarChar, 50).Value = s1;
            da.SelectCommand = command;
            da.Fill(data);
            if (data.Rows.Count > 0) check1 = true;
            return check1;
        }
       
        public String getdate()
        {
            String s;
            s = txtNam.Text + "-" + cbThang.Text+ "-" + txtNgay.Text + " " + cbGio.Text + ":" + cbPhut.Text + ":" + cbGiay.Text;
        return s;
        }
        public void binding()
        {
            txtMaPH.DataBindings.Clear();
            txtMaPH.DataBindings.Add("Text",dataGridView.DataSource,"MaPhieuHen");
            txtMaNV.DataBindings.Clear();
            txtMaNV.DataBindings.Add("Text",dataGridView.DataSource,"MaNV");
            txtTenNV.DataBindings.Clear();
            txtTenNV.DataBindings.Add("Text", dataGridView.DataSource, "TenNV");
            dateTimePicker1.DataBindings.Clear();
            dateTimePicker1.DataBindings.Add("Text", dataGridView.DataSource, "NgayKiemTra");
            txtPhongKT.DataBindings.Clear();
            txtPhongKT.DataBindings.Add("Text",dataGridView.DataSource,"PhongKiemTra");
        }

        private void bt_NhapMoi_Click(object sender, EventArgs e)
        {
            txtMaPH.Enabled = true; 
            txtMaNV.Text = "";
            txtMaPH.Text = "";
            dateTimePicker1.Text = "";
            txtPhongKT.Text = "";
            txtTenNV.Text = "";
            txtNam.Text = "";
            txtNgay.Text = "";
            bt_Xoa.Enabled = false;
            button2.Enabled = false;
            
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaPhieuHen like '" + txtMaPH.Text + "'")[0];
            row.BeginEdit();
            row.Delete();
            row.EndEdit();
            SqlCommand commandDelete = new SqlCommand();
            commandDelete.Connection = conn.con;
            commandDelete.CommandType = CommandType.Text;
            commandDelete.CommandText = @"Delete from PhieuHen Where MaPhieuHen=@MaPhieuHen";
            commandDelete.Parameters.Add("@MaPhieuHen", SqlDbType.NVarChar, 50, "MaPhieuHen");
            da.DeleteCommand = commandDelete;
            da.Update(dt);
            MessageBox.Show("ban da xóa thanh cong", "chuc mung", MessageBoxButtons.OK);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaPhieuHen like '" + txtMaPH.Text + "'")[0];
            row.BeginEdit();
            row["MaNV"] = txtMaNV.Text;
            row["TenNV"] = txtTenNV.Text;
            row["NgayKiemTra"] = dateTimePicker1.Text;
            row["PhongKiemTra"] = txtPhongKT.Text;
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = conn.con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update PhieuHen Set TenNV= @TenNV,
                                                              MaNV= @MaNV,
                                                              NgayKiemTra= @NgayKiemTra,
                                                              PhongKiemTra= @PhongKiemTra
                                                              where MaPhieuHen= @MaPhieuHen";
            commandUpdate.Parameters.Add("@MaPhieuHen", SqlDbType.NVarChar, 50, "MaPhieuHen");
            commandUpdate.Parameters.Add("@TenNV", SqlDbType.NVarChar, 50, "TenNV");
            commandUpdate.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
            commandUpdate.Parameters.Add("@NgayKiemTra", SqlDbType.Date, 20, "NgayKiemTra");
            commandUpdate.Parameters.Add("@PhongKiemTra", SqlDbType.NVarChar, 50, "PhongKiemTra");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("ban da chinh sua thanh cong", "chuc mung", MessageBoxButtons.OK);
        }
        private void PhieuHen_Load(object sender, EventArgs e)
        {
            conn.connect();
            getdata();
            binding();
        }
        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaPH.Enabled = false;
        }

        private void bt_Reset_Click(object sender, EventArgs e)
        {
            txtMaPH.Enabled = false;
            dt.Clear();
            getdata();
            bt_Xoa.Enabled = true;
            button2.Enabled = true;
        }
    }
}
